﻿using System;
using System.Collections.ObjectModel;

namespace RHLogReader.Model
{
    public class Message
    {
        private DateTime _Date;
        public DateTime Date
        {
            get { return _Date; }
            set { _Date = value; }
        }

        private Character _Character;
        public Character Character
        {
            get { return _Character; }
            set { _Character = value; }
        }

        private string _Text = "NULL";
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }

        private Channel _ChannelType;
        public Channel ChannelType
        {
            get { return _ChannelType; }
            set { _ChannelType = value; }
        }

        private ObservableCollection<Group> _Groups;
        public ObservableCollection<Group> Groups
        {
            get { return _Groups; }
            set { _Groups = value; }
        }

        string originalLine;

        private bool _IsVisible = false;
        public bool IsVisible
        {
            get { return _IsVisible; }
            set { _IsVisible = value; }
        }

        //ObservableCollection<string> NPCs = new ObservableCollection<string>() { "Recruitment Officer Blythe", "James Deacon", "Wendy Darren" };

        //ObservableCollection<string> SystemYourCommunicates = new ObservableCollection<string>() { "" };

        //ObservableCollection<string> SystemUserCommunicates = new ObservableCollection<string>() { "" };

        public Message(DateTime date, Character character, string text, Channel channelType, bool isVisible, Group initialGroup = null)
        {
            this.Date = date;
            this.Character = character;
            this.Text = text;
            this.ChannelType = channelType;
            this.IsVisible = isVisible;

            Groups = new ObservableCollection<Group>();

            if (initialGroup != null)
                Groups.Add(initialGroup);
        }

        ///// <summary>
        ///// Creates Message object
        ///// </summary>
        ///// <param name="text">Line which will be parsed</param>
        //public Message(string text)
        //{
        //    originalLine = text;

        //    if (originalLine != null && originalLine.CompareTo(String.Empty) != 0)
        //    {
        //        ParseText(originalLine);
        //    }
        //}

        ///// <summary>
        ///// Creates Message object
        ///// </summary>
        ///// <param name="date">Retireved date from the line</param>
        ///// <param name="text">Line from which Character will be retrieved</param>
        //public Message(DateTime date, string text)
        //{
        //    this.Date = date;

        //    string lineToBeParsed = text;
        //}

        //private void ParseText(string text)
        //{
        //    // Get date for line
        //    int daySeparator = text.IndexOf('/');
        //    int month = Int32.Parse(text.Substring(0, daySeparator));

        //    int monthSeparator = text.IndexOf(" ");
        //    int day = Int32.Parse(text.Substring(daySeparator + 1, monthSeparator - daySeparator));

        //    int hour = Int32.Parse(text.Substring(monthSeparator + 1, 2));
        //    int minute = Int32.Parse(text.Substring(monthSeparator + 4, 2));
        //    int second = Int32.Parse(text.Substring(monthSeparator + 7, 2));

        //    Date = new DateTime(DateTime.Now.Year, month, day, hour, minute, second);

        //    int dateSeparator = text.IndexOf("  ");
        //    string lineToBeParsed = text.Substring(dateSeparator + 2);

        //    /* Recognize character name
        //     * Types for Character = "You":
        //     * Joined Channel
        //     * To 
        //     * You 
        //     * 
        //     * Types for Character = "System":
        //     * |Hchannel
        //     * 
        //     * Types to ignore:
        //     * Changed Channel:
        //     * 
        //     * Channel types:
        //     * says: - Say
        //     * whispers: - Whisper
        //     * To <imie>: - Whisper
        //     * yells: - Yell
        //     * Imie bez separatorow - Emote
        //     * [5. Noir] - kanal Noir
        //     * |Hchannel:GUILD|h[Guild]|h - Guild
        //     * |Hchannel:PARTY|h[Party]|h - Party
        //    */

        //    // Say
        //    if (lineToBeParsed.Contains("says:"))
        //    {
        //        ChannelType = new Channel() { Name = "Say" };

        //        GetBasicData(lineToBeParsed, " says: ");
        //    }
        //    // Whisp from
        //    else if (lineToBeParsed.Contains("whispers:"))
        //    {
        //        ChannelType = new Channel() { Name = "Whisp" };

        //        GetBasicData(lineToBeParsed, " whispers: ");
        //    }
        //    // Whisp to
        //    else if (Regex.IsMatch(lineToBeParsed, "^To"))
        //    {
        //        ChannelType = new Channel() { Name = "Whisp" };

        //        GetBasicData(lineToBeParsed, ": ");
        //    }
        //    // Yell
        //    else if (lineToBeParsed.Contains("yells:"))
        //    {
        //        ChannelType = new Channel() { Name = "Yell" };

        //        GetBasicData(lineToBeParsed, " yells: ");
        //    }
        //    // Guild
        //    else if (lineToBeParsed.Contains("|Hchannel:GUILD|h[Guild]|h"))
        //    {
        //        ChannelType = new Channel() { Name = "Guild" };

        //        lineToBeParsed = lineToBeParsed.Substring(27);

        //        GetBasicData(lineToBeParsed, ": ");
        //    }
        //    // Party member
        //    else if (lineToBeParsed.Contains("|Hchannel:PARTY|h[Party]|h"))
        //    {
        //        ChannelType = new Channel() { Name = "Party" };

        //        lineToBeParsed = lineToBeParsed.Substring(27);

        //        GetBasicData(lineToBeParsed, ": ");
        //    }
        //    // Party leader
        //    else if (lineToBeParsed.Contains("|Hchannel:PARTY|h[Party Leader]|h"))
        //    {
        //        ChannelType = new Channel() { Name = "Party" };

        //        lineToBeParsed = lineToBeParsed.Substring(33);

        //        GetBasicData(lineToBeParsed, ": ");
        //    }
        //    // najpierw rozpoznawanie kanalu
        //    else if (lineToBeParsed.StartsWith("[") && Regex.IsMatch(lineToBeParsed, @"^[[]\d+"))
        //    {
        //        ChannelType = new Channel() { Name = lineToBeParsed.Substring(lineToBeParsed.IndexOf(" ") + 1, lineToBeParsed.IndexOf("]") - lineToBeParsed.IndexOf(" ") - 1) };

        //        lineToBeParsed = lineToBeParsed.Substring(lineToBeParsed.IndexOf("]") + 2);

        //        if (Regex.IsMatch(lineToBeParsed, @"^\S+[:]"))
        //            GetBasicData(lineToBeParsed, ": ");
        //        else
        //            GetBasicData(lineToBeParsed, " ");
        //    }
        //    // pozniej cala reszta. Rozpoznawac komunikaty systemowe i rozdzielac je od emot
        //    else
        //    {
        //        ChannelType = new Channel() { Name = "Other" };
        //        Text = lineToBeParsed;
        //        IsVisible = true;
        //    }

        //}

        //void GetBasicData(string lineToBeParsed, string channelTemplate)
        //{
        //    int channelSeparator = lineToBeParsed.IndexOf(channelTemplate);
        //    Character = lineToBeParsed.Substring(0, channelSeparator);

        //    Text = lineToBeParsed.Substring(channelSeparator + channelTemplate.Length);
        //    //IsVisible = !IsNPC(Character);
        //}

        //bool IsNPC(string name)
        //{
        //    for (int i = 0; i < NPCs.Count; i++)
        //    {
        //        if (NPCs[i].CompareTo(Character) == 0)
        //        {
        //            return true;
        //        }
        //    }

        //    return false;
        //}

        public override string ToString()
        {
            if (IsVisible)
                return "[" + Date.ToString("dd.MM HH:mm:ss") + "] [" + ChannelType.Name + "] " + Character + ": " + Text;
            else return "";
        }
    }
}
